<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        添加收款合同
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../css/main.css" media="all">
    <link rel="stylesheet" href="../../../../lib/layui/css/layui_2.5.7.css">
    <script type="text/javascript" src="../../../../lib/loading/okLoading.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">

</head>

<style>
    /* .layui-form-label {
        width: 112px !important;
    } */
    .layui-tab {
        /* text-align: left !important;
        margin: 10px 0px; */
        overflow: auto !important;
    }
    .layui-upload-file {
        height: 0 !important;
    }

</style>

<body>
<div class="x-body">
    <form class="layui-form layui-form-pane" id="add">
        <div class="layui-tab-content">
            <span style="font-weight:bold">基本信息</span>
            <div class="layui-form-item" style="margin-top: 8px;">
                <label class="layui-form-label">合同编号</label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="text" name="contractNum" lay-filter="contractNum" lay-verify="" placeholder="请输入合同编号" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">合同类型<span style="color: red;">*</span></label>
                <div class="layui-input-block">
                    <select name="contractType" lay-filter="contractType" lay-verify="required">
                        <option value=""></option>
                        <option value="1">销售合同</option>
                        <option value="2">工程合同</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">合同总额<span style="color: red;">*</span></label>
                <div class="layui-input-block">
                    <input  autocomplete="off"  type="number" name="contractTotalAmount" lay-verify="required" lay-filter="contractTotalAmount" placeholder="合同总额/元" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">关联项目<span style="color: red;">*</span></label>
                <div class="layui-input-block">
                    <select id="projectId" name="projectId" lay-filter="projectId" lay-verify="required">
                        <option value="">请选择关联项目</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">签订日期</label>
                    <div class="layui-input-block">
                        <input  autocomplete="off"  type="text" name="signTime" id="signTime" lay-filter="signTime" class="layui-input" placeholder="请选择签订日期" style="width:247px;">
                    </div>
                </div>
                <div class="layui-inline">
                <label class="layui-form-label">合同周期</label>
                <div class="layui-input-inline">
                    <input  autocomplete="off"  type="number" name="contractCycle" class="layui-input" placeholder="单位：天" style="width:245px;">
                </div>
                </div>

            </div>

            <span style="font-weight:bold">甲方乙方</span>

            <div class="layui-form-item" style="margin-top: 8px;">
                <label class="layui-form-label">甲方联系人</label>
                <div class="layui-input-block">
                    <select id="partyALinkmanId" name="partyALinkmanId" lay-filter="">
                        <option value="">请选择甲方联系人</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">乙方联系人</label>
                <div class="layui-input-block" style="height: 10px;">
                    <select name="partyBLinkmanId" id="partyBLinkmanId" lay-filter="" lay-search="">
                        <option value="">直接选择或搜索选择</option>
                    </select>
                    </div>
            </div>

            <!-- <span style="font-weight:bold">账单分期设置<span style="color: red;">*</span></span>
            <div class="layui-form-item" style="margin-top: 8px;">
            </div> -->
            <span style="font-weight:bold">备注信息</span>
            <div class="layui-form-item layui-form-text" style="margin-top: 8px;">
                <div class="layui-input-block">
                    <textarea name="memo" placeholder="合同中一些重要信息或者与甲方的约定等相关信息。" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>
        <div style="text-align: center" class="layui-form-item">
            <button class="layui-btn" type="button" lay-filter="sub" lay-submit="sub">
                保存
            </button>
        </div>
    </form>
    

</div>

<script type="text/html" id="barDemo">
    <a title="编辑" lay-event="view" class="ml-5 layui-btn-xs layui-btn" style="text-decoration:none">编辑</a>
    <a title="删除" href="javascript:;" class="layui-btn-xs layui-btn layui-btn-danger" lay-event="del"
       style="text-decoration:none">
        删除
    </a>
</script>

<script type="text/javascript" src="../../../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../../../js/myAjax.js"></script>
<script type="text/javascript" src="../../../../js/pub.js"></script>
<script src="../../../../lib/layui/layui2.5.7.js" charset="utf-8"></script>
<script src="../../../../js/x-layui.js" charset="utf-8"></script>
<script src="../../../../js/vue.min.js" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>

<script>
    layui.use(['element', 'layer', 'form', 'laydate'], function () {
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        laydate = layui.laydate;//日期插件
        form = layui.form;
        okLoading.close($);
    })

</script>
<script>

    layui.use(['laydate', 'table','form', 'layer','upload'],
        function () {
            var laydate = layui.laydate;
            var $ = layui.jquery;
            var form = layui.form;
            var layer = layui.layer;
            // var element = layui.element;
            var table = layui.table;
            var upload = layui.upload;

            // var contractFilUrlAll = null ;
            var contractFilUrlArrayObj = new Array();　//创建一个数组
            getPartyBLinkmanId();
            var hetongTypeVal = '';

            laydate.render({
                elem: '#dueDate', //指定元素
                trigger: 'click',
                type: 'date',
                done: function (value, date, endDate) {
                    vm.rp.dueDate = value
                }
            });
            laydate.render({
                elem: '#signTime'
            });

            //监听类型选择
            form.on('select(contractType)', function(data) {
                getProject(data.value);
                setTimeout(() => {
                    form.render('select');
                }, 200);
            });

            //监听项目选择
            form.on('select(projectId)', function(data) {
                getPartyALinkmanId(data.value);
                setTimeout(() => {
                    form.render('select');
                }, 200);
            });

            //监听审批提交
            form.on('submit(sub)',
                function (data) {
                    var val = data.field;
                    val.userId = $userId;
                    val.contractNature = 1;

                    let request = {
                        url: 'xa/contractInfo/addContractInfo',
                        data: val,
                        method: "post",
                    };
                    sendRequest(request, (data) => {
                        if (data.code === 0) {
                            layer.alert("操作成功", {
                                    icon: 6
                            },
                            function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.location.reload();

                            });
                        } else {
                            layer.alert(data.msg, {
                                icon: 5
                            })
                        }
                    });
                    return false;
                });


        });

    function getProject(typeId){
        document.getElementById("projectId").innerHTML = "";
        $.ajax({
        	type:"get",
            url:baseHead + "xa/project/findProjectListByTypeIdAndDelFlag",
            data:{
                userId :$userId,
                delFlag:0,
                businessTypeId:typeId
            },
            headers: {
                token: $token
            },
            success:function(res){
               	if(res.code == 0){
                    $("#projectId").append("<option value=''>请选择关联项目</option>");
                    for(var i =0;i < res.data.length;i++){
                        $("#projectId").append("<option value=\""+res.data[i].id+"\">"+res.data[i].name+"</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }else{
                    layer.msg(res.message);
                }
            }
        });
    };
    
    function getPartyALinkmanId(projectId){
        document.getElementById("partyALinkmanId").innerHTML = "";
        $.ajax({
        	type:"get",
            url:baseHead + "xa/project/findProjectLinkmanInfoOneType",
            data:{
                userId :$userId,
                projectId:projectId,
                businessTypeId:0
            },
            headers: {
                token: $token
            },
            success:function(res){
               	if(res.code == 0){
                    $("#partyALinkmanId").append("<option value=''>请选择甲方联系人</option>");
                    
                    for(var i =0;i < res.data.length;i++){
                        $("#partyALinkmanId").append("<option value=\""+res.data[i].id+"\">"+res.data[i].linkmanName+"</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }else{
                    layer.msg(res.message);
                }
            }
        });
    };
    function getPartyBLinkmanId(){
        // document.getElementById("partyALinkmanId").innerHTML = "";
        $.ajax({
        	type:"get",
            url:baseHead + "user/findUserSelect",
            data:{
                userId :$userId,
            },
            headers: {
                "token": $token,
            },
            success:function(res){
               	if(res.code == 0){
                    // $("#partyALinkmanId").append("<option value=''>请选择甲方联系人</option>");
                    
                    for(var i =0;i < res.data.length;i++){
                        $("#partyBLinkmanId").append("<option value=\""+res.data[i].id+"\">"+res.data[i].nickname+"</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }else{
                    layer.msg(res.message);
                }
            }
        });
    }

    const vm = new Vue({
        el: "#app",
        data: {
            projectId:[],
            rp: {
                id: null,
                billType: null,
                billAmount: null,
                dueDate: null,
                personInChargeOfTheBill: null,
                relatedCustomers: null,
                associatedItems: null,
                relatedContract: null,
                payer: null,
                remark: null,
                type: null,
                skUnit: null,
                yfDate: null,
            }
        },
        created() {

            // let request = {
            //     url: 'xa/project/findProjectListByTypeIdAndDelFlag',
            //     data: {
            //         userId: $userId,
            //         delFlag:0,
            //         businessTypeId:1
            //     },
            //     method: "get"
            // }
            // sendRequest(request, (res) => {
            //     if(res.code == 0){
            //         for(var i =0;i < res.data.length;i++){
            //             $("#projectListBycontractType").append("<option value=\""+res.data[i].id+"\">"+res.data[i].name+"</option>");
            //         }
            //         //重新渲染
            //         layui.form.render("select");
            //     }else{
            //         layer.msg(res.message);
            //     }
            // })

        },
        computed: {},
        methods: {
            
        },


    });

    setTimeout(() => {
        //修改状态
        let form = layui.form;
        //账单类型
        let request = {
            url: 'dictionary/findDataDictionaryValue',
            data: {
                userId: $userId,
                label: 'BILL_TYPE'
            },
            method: "POST",
        };
        sendRequest(request, (res) => {
            let x = res.data;
            $.each(x, function (index, item) {
                let opt = $("<option value=" + item + ">" + item + "</option>");
                $("select[name=billType]").append(opt);

            });

            $("select[name=billType]").val(vm.rp.billType);

            form.render('select');
        });
    }, 300)

</script>

</body>

</html>
